<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/24 0024
 * Time: 上午 10:25
 * Author: NanTao <260053530@qq.com>
 */

namespace app\index\controller;
use think\Db;
class Sign extends Common
{
    public function index()
    {
        $userId = session('userId');
        $sign = Db::name('user_sign')->where('user_id',$userId)->whereTime('add_time','d')->find();
        $this->assign('sign',$sign);


        $upper = Db::name('user_sign')->where('user_id',$userId)->order('add_time desc')->find();
        if (!$upper || (strtotime(date('Y-m-d',time())) - strtotime($upper['add_time'])) > 24 * 3600) {
            $day = 0;
        } else {
            $day = $upper['day'];
        }

        $this->assign('day',$day);
        $point = Db::name('user_point')->where(['type'=>2,'user_id'=>$userId])->sum('count');
        $this->assign('point',$point);

        return $this->fetch();
    }

    public function sign()
    {
    	$userId = session('userId');
        $user = get_user_model($userId);
        if ($user['type'] != 1) {
            json_msg(1,'您还不是代言人');
        }
        if (Db::name('user_sign')->where('user_id',$userId)->whereTime('add_time','d')->find()){
            json_msg(1,'您今天已经签到过了,明天再来吧!');
        }
        $upper = Db::name('user_sign')->where('user_id',$userId)->order('add_time desc')->find();
        if (!$upper || (strtotime(date('Y-m-d',time())) - strtotime($upper['add_time'])) > 24 * 3600 || $upper['day'] == 7) {
            $point = sysconf('sign_one');
            $day = 1;
        } else {
            $day = $upper['day'] + 1;
            if($day == 2){
                $point = sysconf('sign_two');
            }else if($day == 3){
                $point = sysconf('sign_three');
            } else if ($day == 4) {
                $point = sysconf('sign_four');
            } else if ($day == 5) {
                $point = sysconf('sign_five');
            } else if ($day == 6) {
                $point = sysconf('sign_six');
            } else if ($day == 7) {
                $point = sysconf('sign_seven');
            }
        }
        $data = [
            'user_id'=>$userId,
            'day'=>$day,
            'point'=>$point
        ];
        Db::name('user_sign')->insert($data);
        $return = post_point($userId,$point,"连续签到{$day}天获得{$point}积分",2,'inc');
        if ($return) {
            json_msg_data(0,'签到成功!',['day'=>$day,'point'=>$point]);
        } else {
            json_msg(1,'签到失败');
        }
    }
}